﻿using System.Collections;
using System.IO.Ports;
using TYBase;

namespace TYRTUBase
{
    public class TYRTU : ITYClient
    {

        public override string ParaKey()
        {
            return $"{Port}";
        }

        public int Port { get; private set; }
        public int BaudRate { get; private set; }
        public Parity Parity { get; private set; }
        public int DataBits { get; private set; }
        public StopBits StopBits { get; private set; }
        public override bool IsConnected
        {
            get { return pcomm != null && pcomm.IsOpen; }
        }
        public TYRTU(int _port, ComPara para)
        {
            Port = _port;
            BaudRate = para.baudRate;
            Parity = para.parity;
            DataBits = para.dataBits;
            StopBits = para.stopBits;
        }

        private PComm pcomm;

        public override bool ClearInBuff()
        {
            if (pcomm == null)
                return false;
            return pcomm.DiscardInBuffer();
        }

        public override bool Connect()
        {
            if (pcomm == null)
                pcomm = new PComm();
            Hashtable Ht = new Hashtable
            {
                { "Port", Port.ToString() },
                { "Baud_Rate", BaudRate },
                { "Parity", Parity },
                { "Byte_Size", DataBits },
                { "Stop_Bits", StopBits }
            };
            return pcomm.InitComm(Ht);
        }

        public override void Close()
        {
            if (pcomm == null)
            {
                return;
            }

            pcomm.CloseComm();
        }


        public override int Write(byte[] byteData, int offset = 0, int length = -1)
        {
            if (pcomm == null || byteData == null || pcomm.IsOpen == false)
            {
                return -1;
            }
            if (length == -1)
            {
                pcomm.Write(byteData, offset, byteData.Length);
                FireOnComDataSend(this, byteData);
                return byteData.Length;
            }
            else
            {
                pcomm.Write(byteData, offset, length);
                FireOnComDataSend(this, byteData);
                return length;
            }
        }

        public override int Read(out byte[] byteData, int length = -1)
        {
            if (pcomm == null || pcomm.IsOpen == false)
            {
                byteData = null;
                return -1;
            }

            int readLength = pcomm.DataBufferLength;

            if (readLength > 0)
            {
                byteData = new byte[readLength];
                if (length == -1)
                {
                    if (pcomm.Read(0, readLength, ref byteData))
                    {
                        FireOnComDataRead(this, byteData);
                        return readLength;
                    }
                }
                byteData = new byte[length];
                if (pcomm.Read(0, length, ref byteData))
                {
                    FireOnComDataRead(this, byteData);
                    return length;
                }
            }
            byteData = null;
            return -1;
        }

        public override string GetInfo()
        {
            return "";
        }

        //后续处理得到当前参数的消息,例如端口等消息
        public override string GetPara()
        {
            return "";
        }

    }
}